package cn.broadin.app.hotel.util;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.util.Log;

/**
 * 
 * @author   long
 * @version  1.0.0
 * @2015年3月31日 下午1:36:45
 */
@SuppressLint("DefaultLocale")
public class MacAddressHelper {
	
	private static final String TAG = "MacAddressHelper";
	
	private static MacAddressHelper _instence = new MacAddressHelper();
	private String _macAddress;
	private String _macCardNum;
	
	private MacAddressHelper(){}
	
	public static MacAddressHelper getInstance(){
		return _instence;
	}
	
	/**
	 * 获取MAC地址
	 * 
	 * @author long
	 * @create 2015年3月31日 下午1:51:51
	 * @since 
	 * @param context
	 * @return
	 */
	public String getMacAddress(Context context){
		if(TextUtils.isEmpty(_macAddress)){
			String[] macAddArr = null;
			_macAddress = getLineMacAddress("eth");
			if(TextUtils.isEmpty(_macAddress)){
				WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
				if(wifiMgr.isWifiEnabled()) {
					WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
					if (null != info) {
						_macAddress = info.getMacAddress();
					}
				}
			}
			Log.i(TAG, "mac addr str : " + _macAddress);
			_macAddress = _macAddress.replaceAll("\\.", ":");
			_macAddress = _macAddress.replaceAll("-", ":");
			_macAddress = _macAddress.toLowerCase();
			
			if(_macAddress.indexOf(":") > -1) {
				macAddArr = _macAddress.split(":");
			} else {
				macAddArr = new String[0];
			}
			String tempResult = "";
			for(int i = 0; i < macAddArr.length; i++) {
				tempResult += Integer.parseInt(macAddArr[i], 16) + "";
			}
			_macCardNum = Long.parseLong(tempResult) + "";
		}
		return _macAddress;
	}
	
	/**
	 * 获取CA卡号
	 * 
	 * @author long
	 * @create 2015年3月31日 下午1:51:41
	 * @since 
	 * @param context
	 * @return
	 */
	public String getCardNum(Context context){
		if(TextUtils.isEmpty(_macCardNum)){
			getMacAddress(context);
		}
		return _macCardNum;
	}
	
	/**
	 * 获取有限地址mac地址
	 * 
	 * @author long
	 * @create 2015年3月31日 下午1:39:12
	 * @since 
	 * @param name
	 * @return
	 */
	private String getLineMacAddress(String name) {
		try {
			Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
			while (en.hasMoreElements()) {
				NetworkInterface intf = en.nextElement();
				if (intf.getName().toLowerCase().contains(name)) {
					byte[] ha = intf.getHardwareAddress();
					if (ha != null) {
						StringBuilder sb = new StringBuilder();
						for (int i = 0; i < ha.length; ++i) {
							if (i > 0) {
								sb.append(":");
							}
							sb.append(String.format("%1$02x", ha[i]));
						}
						return sb.toString();
					}
				}
			}
		} catch (SocketException ex) {
			Log.e(TAG, "getLineMacAddress error. " + ex.getMessage());
		}
		return "";
	}

	/**
	 * 获取设备MAC地址
	 * 
	 * @author long
	 * @create 2015年3月31日 下午1:54:31
	 * @since 
	 * @param ctx
	 * @return
	 */
	public String getAllMac(Context ctx){
		StringBuffer urlBuffer = new StringBuffer();
		String mac_line = getLineMacAddress("eth");
		String mac_wifi = "";
		String mac_bt = "";
		try {
			WifiManager wifiMgr = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
			if(wifiMgr.isWifiEnabled()) {
				WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
				if (null != info) {
					mac_wifi = info.getMacAddress();
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		try{
			BluetoothAdapter bAdapt= BluetoothAdapter.getDefaultAdapter();  
			if (bAdapt != null){  
	            mac_bt = bAdapt.getAddress();  
	        }
		}catch(Exception e){
			e.printStackTrace();
		}
		urlBuffer.append("wifi=");
		urlBuffer.append(this.trimMac(mac_wifi));
		urlBuffer.append("&line=");
		urlBuffer.append(this.trimMac(mac_line));
		urlBuffer.append("&lan=");
		urlBuffer.append(this.trimMac(mac_bt));
		
		Log.e(TAG, " wif="+ mac_wifi+";line="+mac_line+";mac_bt="+mac_bt);
		return urlBuffer.toString();
	}
	
	private String trimMac(String macAddress_str){
		String retMac = "";
		try{
			if(!macAddress_str.equals("")){
				macAddress_str = macAddress_str.replaceAll("\\.", ":");
				macAddress_str = macAddress_str.replaceAll("-", ":");
				macAddress_str = macAddress_str.toLowerCase();
			}
		}catch(Exception e){
			Log.d(TAG, "trimMac "+e.getMessage() );
		}
		retMac = macAddress_str;
		return retMac;
	}
}

